home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / text / hyper / ADtoHT2_0.lha / FormatNode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  3.0 KB  |  129 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3.  
  4. /************************************************************************/
  5.  
  6. #include "main.h"
  7. #include "File.h"
  8. #include "FormatNode.h"
  9.  
  10. /************************************************************************/
  11. /*                                                                      */
  12. /* Determine column widths for a node.                                  */
  13. /* Returns a 0-terminated array of column widths                        */
  14. /*                                                                      */
  15. /* The NextButton() function is expected to return the label of the     */
  16. /* label of the next button.                                            */
  17. /* "" tells us to start a new line                                      */
  18. /*                                                                      */
  19. /************************************************************************/
  20.  
  21. int *
  22. FormatNode (char *(*NextLabel) (char *, void *),
  23.         void *Parameters)
  24.  
  25. {
  26.   int *ColWidth;
  27.   int Columns;
  28.   char *ButtonLabel;
  29.  
  30.   Columns = *Arguments.Width / 4;
  31.   ColWidth = xmalloc ((Columns + 1) * sizeof (*ColWidth));
  32.  
  33.   do
  34.     {
  35.       int Column;
  36.       int LineWidth;
  37.  
  38.       memset (ColWidth, 0, (Columns + 1) * sizeof (*ColWidth));
  39.       ButtonLabel = NULL;
  40.  
  41.       Column = 0;
  42.       LineWidth = 0;
  43.       while ((ButtonLabel = NextLabel (ButtonLabel, Parameters)))
  44.     {
  45.       size_t Width;
  46.  
  47.       if ((Width = strlen (ButtonLabel)))
  48.         {
  49.           if (Width > ColWidth[Column])
  50.         ColWidth[Column] = Width;
  51.           LineWidth += ColWidth[Column] + ((*Arguments.Version) >= 39);
  52.           if (LineWidth > *Arguments.Width)
  53.         {
  54.           break;
  55.         }
  56.           Column++;
  57.           if (Column == Columns)
  58.         {
  59.           Column = 0;
  60.           LineWidth = 0;
  61.         }
  62.           else
  63.         {
  64.           LineWidth += 3;
  65.         }
  66.         }
  67.       else
  68.         {
  69.           Column = 0;
  70.           LineWidth = 0;
  71.         }
  72.     }
  73.       free (ButtonLabel);
  74.     }
  75.   while (ButtonLabel && --Columns);
  76.   return ColWidth;
  77. }
  78.  
  79. /************************************************************************/
  80. /*                                                                      */
  81. /* Print a table of buttons.                                            */
  82. /*                                                                      */
  83. /************************************************************************/
  84.  
  85. void 
  86. PrintNode (char *(*NextLabel) (char *, void *),
  87.        void (*PrintLink) (void *),
  88.        void *Parameters, int *ColWidth)
  89.  
  90. {
  91.   int Column;
  92.   char *Label;
  93.  
  94.   Column = 0;
  95.   Label = NULL;
  96.   while ((Label = NextLabel (Label, Parameters)))
  97.     {
  98.       if (Label[0])
  99.     {
  100.       WPrintf ("@{\x22%s\x22 LINK ", Label);
  101.       PrintLink (Parameters);
  102.       WPrintf ("}");
  103.       Column++;
  104.       if (ColWidth[Column])
  105.         {
  106.           int i;
  107.  
  108.           for (i = 3 + ColWidth[Column - 1] - strlen (Label) - (*Arguments.Version >= 39); i > 0; i--)
  109.         WPrintf (" ");
  110.         }
  111.       else
  112.         {
  113.           Column = 0;
  114.           WPrintf ("\n");
  115.         }
  116.     }
  117.       else
  118.     {
  119.       Column = 0;
  120.       WPrintf ("\n");
  121.     }
  122.     }
  123.   free (Label);
  124.   if (Column)
  125.     {
  126.       WPrintf ("\n");
  127.     }
  128. }
  129.